package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.domain.SysSetting;
import com.sanjishu.enterlab.electricity.sale.mapper.SysSettingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SysSettingService {

    @Autowired
    private SysSettingMapper sysSettingMapper;

    public String getNum() {
        SysSetting sysSetting = sysSettingMapper.findOne();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date currentDate = new Date();
        String numString = "";

        if (sysSetting.getContractTime() != null
                && sdf.format(sysSetting.getContractTime()).equals(sdf.format(currentDate))) {
            Integer currentNum = sysSetting.getContractNum() + 1;
            sysSetting.setContractNum(currentNum);
        } else {
            sysSetting.setContractTime(currentDate);
            sysSetting.setContractNum(1);
        }
        numString = sysSetting.getContractNum()>=10 ? sysSetting.getContractNum().toString() : ("0"+sysSetting.getContractNum().toString());
        sysSettingMapper.updateByPrimaryKey(sysSetting);
        return sdf.format(currentDate).substring(2)+numString;
    }

}
